package framework.ia;

import org.newdawn.slick.GameContainer;

import framework.BasicFighter;
import framework.Controls;
import framework.attack.BasicAttack;
import framework.collision.AttackCollisionManager;

import java.util.ArrayList;

public class InstinctBrain extends Brain {

	public BasicFighter fighter2;
	
	/*Initialization Brain parameters*/
	protected static final int MAX_ATTACK_RATIO = 50; /*higher percentage of attack*/
	protected static final int MIN_ATTACK_RATIO = 25; /*lower percentage of attack*/
	protected static final int MAX_PASSIVE_RATIO = 50; /*higher percentage of passive action*/
	protected static final int MIN_PASSIVE_RATIO = 25; /*lower percentage of passive action*/
	protected static final int MAX_DEFENSE_RATIO = 50; /*higher percentage of defense action*/
	protected static final int MIN_DEFENSE_RATIO = 25; /*lower percentage of defense action*/
	private float attackRatioStep;
	//Parameters where are saved ratios
	private int currentAttackRatio=50;
	private int currentPassiveRatio=25;
	private int currentDefenseRatio=25;
	private int distanceRatio;
	
	//ActionList
	private ArrayList<String> actionList;
	private ArrayList<String> observationList;
	
	AttackCollisionManager collisions;

	public InstinctBrain(BasicFighter fighter, BasicFighter fighter2) {
		super(fighter);
		this.fighter2 = fighter2;
		actionList = new ArrayList<String>();
		observationList = new ArrayList<String>();
		attackRatioStep = (MAX_ATTACK_RATIO-MIN_ATTACK_RATIO)/10;
		
	}
	
	
	@Override
	public void update(GameContainer gc, int delta) {
		if(actionList.isEmpty()){
			//for(int i=0;i<100;i++){
				observationList.add(observe(gc,delta));
				
			//}
		}
		
		else{
			switch(actionList.get(0)){
			case "attack" :
				BasicAttack<?> att = getHostFighter().getAttacks().getAttack(Controls.A_P1);
		    	att.setAttackCollisionManager(collisions);
		    	getHostFighter().attack(att);
			break;
			
			case "fireball" :
				BasicAttack<?> fir = getHostFighter().getAttacks().getAttack(Controls.B_P1);
		    	fir.setAttackCollisionManager(collisions);
		    	getHostFighter().attack(fir);
			break;
			
			
			case "forward" :
				getHostFighter().move(getHostFighter().isForward());
			break;

			case "backward" :
				getHostFighter().move(false);
			break;
			
			case "jump" :
				
			break;
			case "doNothing" :
				
			break;
			}
			
		actionList.remove(0);
		}
	}

	protected String observe(GameContainer gc, int delta){
		
		
		float lifeRatio = ((((float)fighter2.getHealthPoint()/10))-(((float)getHostFighter().getHealthPoint()/10)));
		distanceRatio = (int)Math.abs(getHostFighter().getHitBox().getCenterX() - fighter2.getHitBox().getCenterX());
		
		//System.out.println("OBSERVING");
		//Personnal note : shitty mathematic model : greg have to use this brain a little bit more but will be fine for now
		if(lifeRatio<0){
		//Bot have a superior life	
			//System.out.println("WINING");
			
			currentAttackRatio=(MIN_ATTACK_RATIO+((MAX_ATTACK_RATIO-MIN_ATTACK_RATIO)/2))+(int)(Math.abs(lifeRatio)*attackRatioStep);
			currentDefenseRatio=(MIN_DEFENSE_RATIO+((MAX_DEFENSE_RATIO-MIN_DEFENSE_RATIO)/2))-(int)(Math.abs(lifeRatio)*attackRatioStep);
			currentPassiveRatio=(MIN_PASSIVE_RATIO+((MAX_PASSIVE_RATIO-MIN_PASSIVE_RATIO)/2))-(int)(Math.abs(lifeRatio)*attackRatioStep);	
			
			System.out.print("Av Ratio : ");		
			System.out.println(currentPassiveRatio+currentAttackRatio+currentDefenseRatio);
			
			if((currentPassiveRatio+currentAttackRatio+currentDefenseRatio<100)&&(Math.abs(lifeRatio)>4)){
				currentDefenseRatio=currentDefenseRatio-(100-(currentPassiveRatio+currentAttackRatio+currentDefenseRatio));
				currentAttackRatio=currentAttackRatio+(100-(currentPassiveRatio+currentAttackRatio+currentDefenseRatio));
			}
			else if(currentPassiveRatio+currentAttackRatio+currentDefenseRatio>100){
				currentAttackRatio=currentAttackRatio+(100-(currentPassiveRatio+currentAttackRatio+currentDefenseRatio));
			}
		
		}
		else if(lifeRatio==0){
		//opponents have equal life
			currentAttackRatio=MIN_ATTACK_RATIO;
			currentDefenseRatio=MIN_DEFENSE_RATIO;
			currentPassiveRatio=((MAX_PASSIVE_RATIO));		
			
		}
		else{
		//Bot have an inferior life
			//System.out.println("LOSING");			

			currentAttackRatio=(MIN_ATTACK_RATIO+((MAX_ATTACK_RATIO-MIN_ATTACK_RATIO)/2))-(int)(lifeRatio*attackRatioStep);
			currentDefenseRatio=(MIN_DEFENSE_RATIO+((MAX_DEFENSE_RATIO-MIN_DEFENSE_RATIO)/2))+(int)(lifeRatio*attackRatioStep);
			currentPassiveRatio=(MIN_PASSIVE_RATIO+((MAX_PASSIVE_RATIO-MIN_PASSIVE_RATIO)/2))-(int)(lifeRatio*attackRatioStep);
			
				if((currentPassiveRatio+currentAttackRatio+currentDefenseRatio<100)&&(lifeRatio>4)){
					currentAttackRatio=currentAttackRatio-(100-(currentPassiveRatio+currentAttackRatio+currentDefenseRatio));
					currentDefenseRatio=currentDefenseRatio+(100-(currentPassiveRatio+currentAttackRatio+currentDefenseRatio));
				}
				else if(currentPassiveRatio+currentAttackRatio+currentDefenseRatio>100){
					currentDefenseRatio=currentDefenseRatio+(100-(currentPassiveRatio+currentAttackRatio+currentDefenseRatio));
				}
			
		}
		

		
		/*int randomNum = (int)(Math.random()*100);
		if(randomNum<currentDefenseRatio){
			//Use a defenseAction
			
			return "defense";
		}
		else if((randomNum>currentDefenseRatio)&&randomNum<(currentDefenseRatio+currentPassiveRatio)){
			//Use a PassiveAction
			
			return "passive";
		}
		else{
			//Use an AttackAction
			
			return "attack";
	
		}*/
		
			int randomNum = (int)(Math.random()*100);
			if(randomNum<currentDefenseRatio){
				//Use a defenseAction
				
				/*System.out.println("IN DEFENSE");*/
				//for(int i=0;i<3;i++)actionList.add("fireball");
				if(getHostFighter().getHealthPoint()<50){
					if(distanceRatio>500){
						for(int i=0;i<5;i++)actionList.add("backward");
						actionList.add("fireball");
					}
					else {
						for(int i=0;i<5;i++)actionList.add("backward");
						actionList.add("attack");
					}
				}
				return "defense";
			}
			else if((randomNum>currentDefenseRatio)&&randomNum<(currentDefenseRatio+currentPassiveRatio)){
				//Use a PassiveAction
				
				//if(distanceRatio>500){
				//actionList.add("fireball");
				//for(int i=0;i<5;i++)actionList.add("forward");
				//}
				//else for(int i=0;i<5;i++)actionList.add("forward");
				for(int i=0;i<5;i++)actionList.add("doNothing");
				return "passive";
			}
			else{
				//Use an AttackAction
				System.out.println("ATTACKING");
				
				if(Math.abs(getHostFighter().getHitBox().getCenterX() - fighter2.getHitBox().getCenterX()) <= 300){
				/*for(int i=0;i<3;i++)*/actionList.add("attack");}
				else {
					actionList.add("fireball");
					for(int i=0;i<(distanceRatio/100);i++)actionList.add("forward");
				}
				return "attack";
			}
		
		
		/*System.out.println("****************");
		System.out.print("Distance Ratio : ");
		System.out.println(distanceRatio);*/
		/*System.out.print("random : ");
		System.out.println(randomNum);
		System.out.print("Bot health : ");
		System.out.println((getHostFighter().getHealthPoint()));
		System.out.print("Host health : ");
		System.out.println(fighter2.getHealthPoint());
		System.out.print("lifeRatio : ");
		System.out.println(lifeRatio);
		System.out.print("attack Ratio : ");
		System.out.println(currentAttackRatio);
		System.out.print("defense Ratio : ");
		System.out.println(currentDefenseRatio);
		System.out.print("passive Ratio : ");		
		System.out.println(currentPassiveRatio);
		System.out.print("Total Ratio : ");		
		System.out.println(currentPassiveRatio+currentAttackRatio+currentDefenseRatio);*/
		//System.out.println("****************");	
		/*attackRatio=(int)(25+((lifeRatio-25)*0.25));
		defenseRatio=(int)(75-((lifeRatio-25)*0.25));
		if(lifeRatio>50)passivityRatio=(int)(50+((lifeRatio+50)*0.25));
		else passivityRatio=(int)(50-((50-lifeRatio)*0.25));*/
		
	    /*if(Math.abs(getHostFighter().getHitBox().getCenterX() - fighter2.getHitBox().getCenterX()) <= 100) {
	    	SwordStrike att = new SwordStrike((Nartan)getHostFighter());
	    	att.setAttackCollisionManager(collisions);
	    	getHostFighter().attack(att);
	    } 
	    else if(fighter2.isAttacking()|fighter2.isJumping()){
	    	//getHostFighter().move(getHostFighter().isJumping());
	    }
	    else {
	    	//getHostFighter().move(getHostFighter().isForward());
	    }*/
	}
	
	protected void choseAction(GameContainer gc, int delta){
		int randomNum = (int)(Math.random()*100);
		if(randomNum<currentDefenseRatio){
			//Use a defenseAction
			
			/*System.out.println("IN DEFENSE");*/
			//for(int i=0;i<3;i++)actionList.add("fireball");
			for(int i=0;i<5;i++)actionList.add("backward");
			actionList.add("fireball");
		}
		else if((randomNum>currentDefenseRatio)&&randomNum<(currentDefenseRatio+currentPassiveRatio)){
			//Use a PassiveAction
			
			if(distanceRatio>500){
			actionList.add("fireball");
			for(int i=0;i<5;i++)actionList.add("forward");
			}
			else for(int i=0;i<5;i++)actionList.add("forward");
		}
		else{
			//Use an AttackAction
			System.out.println("ATTACKING");
			
			if(Math.abs(getHostFighter().getHitBox().getCenterX() - fighter2.getHitBox().getCenterX()) <= 150){
			/*for(int i=0;i<3;i++)*/actionList.add("attack");}
			else for(int i=0;i<5;i++)actionList.add("forward");
		}
	}
	
	public AttackCollisionManager getCollisions() {
		return collisions;
	}

	public void setCollisions(AttackCollisionManager collisions) {
		this.collisions = collisions;
	}
	
}
